package com.px.lovepath.ui.general.pic;

import java.io.File;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.px.lovepath.LovePathEnv;
import com.px.lovepath.R;
import com.px.lovepath.utils.cache.BitmapCache;

public class AvatarView extends FrameLayout {

	private final PicView mAvatarView;
	private final ImageView mAvatarBackgroundView;
//	private final ImageView mAvatarFrameView;
//	private final float mPixelsPerHdpiPixel;
	private String mAvatarUri = null;
	private InputStream mAvatarInputStream = null;
	private String mOnlineAvatarUri = null;
	private Bitmap mAvatarBitmap = null;
	private File mAvatarFile = null;
	private AvatarSource mAvatarSource = AvatarSource.NONE;
	private AvatarSex mAvatarSex = AvatarSex.MALE;
	private static final String AVATAR_MALE_URI = "male.png";
	private static final String AVATAR_FEMALE_URI = "female.png";
	
	public enum AvatarSex {
		MALE,
		FEMALE
	}
	
	private enum AvatarSource {
		NONE,
		LOCAL,
		STREAM,
		BITMAP,
		USER_FILE,
		ONLINE,
		FAILURE
	}
	
	public AvatarView(Context context) {
		this(context, null);
	}
	public AvatarView(Context context, AttributeSet attrs) {
		super(context, attrs);
//		mPixelsPerHdpiPixel = (float) context.getResources().getDisplayMetrics().densityDpi 
//				/ (float) DisplayMetrics.DENSITY_HIGH;
		
		mAvatarBackgroundView = new ImageView(context);
		mAvatarBackgroundView.setScaleType(ScaleType.FIT_XY);
		mAvatarBackgroundView.setImageResource(R.drawable.home__setting_view__avatar_him);
		addView(mAvatarBackgroundView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		
		mAvatarView = new PicView(context);
		mAvatarView.setPicStretch(PicStretch.SCALE_FILL);
		mAvatarView.setIsRoundCorner(true, 10);
		mAvatarView.setPicDecoder(new PicViewDecoder() {
			@Override
			public Bitmap decodePic(String picUri, InputStream picStream) {
				Bitmap picBitmap = null;

				if (mAvatarInputStream != null && mAvatarSource == AvatarSource.STREAM) {
					picBitmap = BitmapFactory.decodeStream(mAvatarInputStream);
				} else if (mAvatarBitmap != null && mAvatarSource == AvatarSource.BITMAP) {
					picBitmap = mAvatarBitmap;
				} 

				if (picBitmap != null && picBitmap.getWidth() > 150) {
					Bitmap scaledBitmap = Bitmap.createScaledBitmap(picBitmap, 150, Math.round(150.0f / picBitmap.getWidth() * picBitmap.getHeight()), false);
					picBitmap.recycle();
					picBitmap = scaledBitmap;
				}
				
				return picBitmap;
			}
		});
		LayoutParams coverViewParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		addView(mAvatarView, coverViewParams);
		
//		mAvatarFrameView = (ImageView) new ImageView(context);
//		mAvatarFrameView.setImageResource(R.drawable.home__home_list_item_view__avatar_bg);
//		addView(mAvatarFrameView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		
		mAvatarUri = LovePathEnv.get().getAvatarDirectory().getAbsolutePath() + File.separator + AVATAR_MALE_URI;
		mAvatarSource = AvatarSource.NONE;
	}
	
	// ### 方法 ###
	public void setAvatarSex(AvatarSex sex) {
		mAvatarSex = sex;
		switch (mAvatarSex) {
		case MALE:
			mAvatarBackgroundView.setImageResource(R.drawable.home__setting_view__avatar_him);
			mAvatarUri = LovePathEnv.get().getAvatarDirectory().getAbsolutePath() + File.separator + AVATAR_MALE_URI;
			break;
		case FEMALE:
			mAvatarBackgroundView.setImageResource(R.drawable.home__setting_view__avatar_her);
			mAvatarUri = LovePathEnv.get().getAvatarDirectory().getAbsolutePath() + File.separator + AVATAR_FEMALE_URI;
		default:
			break;
		}
		mAvatarSource = AvatarSource.LOCAL;
	}
	public void setAvatarInputStream(InputStream inputStream) {
		if (mAvatarInputStream != inputStream) {
			mAvatarInputStream = inputStream;
			resetAvatar();
			mAvatarSource = AvatarSource.STREAM;
			requestLayout();
		}
	}
	public void setAvatarBitmap(Bitmap bitmap) {
		if (mAvatarBitmap != bitmap) {
			mAvatarBitmap = bitmap;
			resetAvatar();
			mAvatarSource = AvatarSource.BITMAP;
			requestLayout();
		}
	}
	public void setAvatarFile(File file) {
		if (mAvatarFile != file) {
			mAvatarFile = file;
			resetAvatar();
			mAvatarSource = AvatarSource.USER_FILE;
			requestLayout();
		}
	}
	public void setOnlineAvatarUri(String onlineAvatarUri) {
		if (mOnlineAvatarUri != onlineAvatarUri) {
			mOnlineAvatarUri = onlineAvatarUri;
			resetAvatar();
			mAvatarSource = AvatarSource.ONLINE;
			requestLayout();
		}
	}
	
	
	// ### 实现 ###
	private void nextCoverSource() {
		mAvatarView.setPicUri(null);
		String temp = new File(mAvatarUri).toURI().toString();
		switch (mAvatarSource) {
		case LOCAL:
			mAvatarView.setPicUri(temp);
			break;
		case STREAM:
			assert mAvatarInputStream != null;
			mAvatarView.setPicUri(temp);
			mAvatarView.setSavePicAs(new File(Uri.parse(mAvatarUri).getPath()));
			break;
		case BITMAP:
			assert mAvatarBitmap != null;
			mAvatarView.setPicUri(temp);
			mAvatarView.setSavePicAs(new File(Uri.parse(mAvatarUri).getPath()));
			break;
		case USER_FILE:
			assert mAvatarFile != null;
			mAvatarView.setPicUri(mAvatarFile.toURI().toString());
			mAvatarView.setSavePicAs(new File(Uri.parse(mAvatarUri).getPath()));
			break;
		case ONLINE:
			// TODO 目前没有网络的情况
			// if (TextUtils.isEmpty(mAvatarUri) == false)
			// mAvatarView.setSavePicAs(new
			// File(Uri.parse(mAvatarUri).getPath()));
			// break;
		case FAILURE:
			break;
		default:
			assert false;
			break;
		}
		invalidate();
	}
	private void resetAvatar() {
		File avatarCacheFile = new File(mAvatarUri);
		if (avatarCacheFile.exists()) {
			PicView.getPicCache().discardSlot(new BitmapCache.CacheKey(avatarCacheFile.toURI().toString()));
		}
	}
	
	// ### View函数重写 ###
	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		if (mAvatarSource != AvatarSource.NONE) {
			nextCoverSource();
		}
		
		super.onLayout(changed, left, top, right, bottom);		
	}
}
